package com.jade.bss.ec.ware.impl;

import java.util.List;

import javax.ejb.TransactionAttribute;
import javax.inject.Inject;
import javax.inject.Named;

import com.jade.bss.ec.ware.*;
import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.Order;
import com.jade.framework.base.criterion.Restrictions;
import com.jade.framework.base.util.PagingList;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品属性管理器实现。
 */
@Named ("bss_wareAttrManager")
public class AttrManagerImpl
        implements WareAttrManager
{
    private WareAttrDao attrDao;

    @Inject
    public void setAttrDao(@Named ("bss_wareAttrDao") WareAttrDao attrDao)
    {
        this.attrDao = attrDao;
    }

    protected void checkGroupName(WareAttrGroup group)
            throws WareException
    {
        Criteria criteria = new Criteria(0, 1);
        criteria.add(Restrictions.eq("cid", group.getCid()));
        criteria.add(Restrictions.eq("name", group.getName()));
        List<WareAttrGroup> groups = attrDao.listGroup(criteria);
        if (!groups.isEmpty() && groups.get(0).getId() != group.getId()) {
            throw new WareException("err.bss.ec.ware.attr.group_name_conflict", "Duplicated attribute group name",
                    null, group.getName());
        }

    }

    @TransactionAttribute
    @Transactional (rollbackFor = Throwable.class)
    public void addGroup(WareAttrGroup group)
            throws WareException
    {
        //同一个商品分类下，属性分组名称不能重复
        checkGroupName(group);
        attrDao.addGroup(group);
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Throwable.class)
    public void updateGroup(WareAttrGroup group)
            throws WareException
    {
        //同一个商品分类下，属性分组名称不能重复
        checkGroupName(group);
        attrDao.addGroup(group);
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Throwable.class)
    public void removeGroup(long... ids)
            throws WareException
    {
        for (long id : ids) {
            WareAttrGroup group = getGroup(id);
            if (group != null) {
                attrDao.removeGroup(group);
            }
        }
    }

    public WareAttrGroup getGroup(long id)
            throws WareException
    {
        return attrDao.getGroup(id);
    }

    public List<WareAttrGroup> listGroup(long cid)
    {
        Criteria criteria = new Criteria();
        criteria.addOrder(Order.asc("sortCode"));
        return listGroup(cid, criteria);
    }

    public List<WareAttrGroup> listGroup(long cid, Criteria criteria)
    {
        criteria.add(Restrictions.eq("cid", cid));
        return attrDao.listGroup(criteria);
    }

    public PagingList<WareAttrGroup> queryGroup(long cid, Criteria criteria)
    {
        criteria.add(Restrictions.eq("cid", cid));
        return attrDao.queryGroup(criteria);
    }

    protected void checkAttrName(WareAttribute attribute)
            throws WareException
    {
        Criteria criteria = new Criteria(0, 1);
        criteria.add(Restrictions.eq("cid", attribute.getCid()));
        criteria.add(Restrictions.eq("name", attribute.getName()));
        List<WareAttribute> attrs = attrDao.listAttr(criteria);
        if (!attrs.isEmpty() && attrs.get(0).getId() != attribute.getId()) {
            throw new WareException("err.bss.ec.ware.attr.name_conflict", "Duplicated attribute name",
                    null, attribute.getName());
        }

    }

    @TransactionAttribute
    @Transactional (rollbackFor = Throwable.class)
    public void addAttr(WareAttribute attribute)
            throws WareException
    {
        //校验属性名称是否重复
        checkAttrName(attribute);
        attrDao.addAttr(attribute);
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Throwable.class)
    public void updateAttr(WareAttribute attribute)
            throws WareException
    {
        //校验属性名称是否重复
        checkAttrName(attribute);
        attrDao.updateAttr(attribute);
    }

    @TransactionAttribute
    @Transactional (rollbackFor = Throwable.class)
    public void removeAttr(long... ids)
            throws WareException
    {
        for (long id : ids) {
            WareAttribute attr = getAttr(id);
            if (attr != null) {
                attrDao.removeAttr(attr);
            }
        }

    }

    public WareAttribute getAttr(long id)
            throws WareException
    {
        return attrDao.getAttr(id);
    }

    public List<WareAttribute> listAttr(long cid)
    {
        Criteria criteria = new Criteria();
        criteria.add(Restrictions.eq("wareId", 0));
        criteria.addOrder(Order.asc("sortCode"));
        return listAttr(cid, criteria);
    }

    public List<WareAttribute> listAttr(long cid, long wareId)
    {
        Criteria criteria = new Criteria();
        criteria.add(Restrictions.eq("wareId", wareId));
        criteria.addOrder(Order.asc("sortCode"));
        return attrDao.listAttr(criteria);
    }

    public List<WareAttribute> listAttr(long cid, Criteria criteria)
    {
        criteria.add(Restrictions.eq("cid", cid));
        return attrDao.listAttr(criteria);
    }

    public PagingList<WareAttribute> queryAttr(long cid, Criteria criteria)
    {
        criteria.add(Restrictions.eq("cid", cid));
        return attrDao.queryAttr(criteria);
    }
}
